package cn.edu.ctbu.studentmanage.service.impl;

import cn.edu.ctbu.studentmanage.domain.Student;
import cn.edu.ctbu.studentmanage.repository.StudentRepository;
import cn.edu.ctbu.studentmanage.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    /**
     * 查询所有的学习信息
     * @return
     */
    public List<Student> findAll(){

        return studentRepository.findAll();
    }
    /**
     * 根据学号查询学生
     * @param sno
     * @return
     */
    public  Student findBySno(String sno){
        return studentRepository.findStudentBySno(sno);
    }

    /**
     * 根据学生学号和密码查询学生
     * @param sno
     * @param spassword
     * @return
     */
    public List<Student> findBySnoAndSpassword(String sno, String spassword){

        List<Student> students =  studentRepository.findBySnoAndSpassword(sno,spassword);

        return students;

    }

    /**
     * 分页
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable) {

        return studentRepository.findAll(pageable);
    }
    /**
     * 分页加动态查询
     * @param studentExample
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> studentExample, Pageable pageable){
        return studentRepository.findAll(studentExample,pageable);
    }
    /**
     * 根据id获取某一学生信息
     * @param sid
     * @return
     */
    public Student getBySid(Integer sid){

        return studentRepository.findById(sid).orElse(null);
    }
    /**
     * 更新学生信息
     * @param student
     */
    public void update(Student student){
        studentRepository.save(student);
    }
    /**
     * 根据id删除某一学生信息
     * @param sid
     */
    public void delete(Integer sid){
        studentRepository.deleteById(sid);
    }
    /**
     * 插入新的学生
     * @param student
     */
    public void Insert(Student student){
        studentRepository.save(student);
    }

}
